package com.reny.szkj.ft_home.widget;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.TextView;

import androidx.annotation.ColorInt;
import androidx.annotation.DrawableRes;
import androidx.core.content.ContextCompat;

import com.airbnb.lottie.LottieAnimationView;
import com.reny.szkj.ft_home.R;

import me.majiajie.pagerbottomtabstrip.internal.RoundMessageView;
import me.majiajie.pagerbottomtabstrip.item.BaseTabItem;

/**
 * Created by mjj on 2017/6/3
 */
public class SpecialTab extends BaseTabItem {

    private LottieAnimationView mIcon;
    private final TextView mTitle;
    private final RoundMessageView mMessages;

    private Drawable mDefaultDrawable;
    private Drawable mCheckedDrawable;
    private String mLottiePath;

    private int mDefaultTextColor = 0x56000000;
    private int mCheckedTextColor = 0x56000000;

    private boolean mChecked;
    //属性动画
    private AnimatorSet mAnimatorSet;
    private final long mDuration = 300;

    public SpecialTab(Context context) {
        this(context, null);
    }

    public SpecialTab(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public SpecialTab(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        LayoutInflater.from(context).inflate(R.layout.view_special_tab, this, true);
        mIcon = findViewById(R.id.icon);
        mIcon.setRepeatCount(0);
        mTitle = findViewById(R.id.title);
        mMessages = findViewById(R.id.messages);
    }

    /**
     * 方便初始化的方法
     *
     * @param drawableRes        默认状态的图标
     * @param checkedDrawableRes 选中状态的图标
     */
    public void initialize(@DrawableRes int drawableRes, @DrawableRes int checkedDrawableRes, String title) {
        mDefaultDrawable = ContextCompat.getDrawable(getContext(), drawableRes);
        mCheckedDrawable = ContextCompat.getDrawable(getContext(), checkedDrawableRes);
        mTitle.setText(title);
    }

    public void initialize(@DrawableRes int drawableRes, String lottiePath, String title) {
        mDefaultDrawable = ContextCompat.getDrawable(getContext(), drawableRes);
        mLottiePath = lottiePath;
        mTitle.setText(title);
    }

    private void initValueAnimator(){
        if(mAnimatorSet == null){
            ObjectAnimator objectAnimator1 = ObjectAnimator.ofFloat(mIcon, SCALE_X, 1.0f, 1.2f, 1.0f);
            ObjectAnimator objectAnimator2 = ObjectAnimator.ofFloat(mIcon, SCALE_Y, 1.0f, 1.2f, 1.0f);
            mAnimatorSet = new AnimatorSet();
            mAnimatorSet.playTogether(objectAnimator1, objectAnimator2);
            mAnimatorSet.setDuration(mDuration);
        }
    }

    @Override
    public void setChecked(boolean checked) {
        if (checked) {
            if(null != mCheckedDrawable) {
                mIcon.setImageDrawable(mCheckedDrawable);
                initValueAnimator();
                mAnimatorSet.start();
            }
            if(!TextUtils.isEmpty(mLottiePath)){
                mIcon.setAnimation(mLottiePath);
                mIcon.playAnimation();
            }
            mTitle.setTextColor(mCheckedTextColor);
        } else {
            if(!TextUtils.isEmpty(mLottiePath)){
                mIcon.clearAnimation();
            }
            mIcon.setImageDrawable(mDefaultDrawable);
            mTitle.setTextColor(mDefaultTextColor);
        }
        mChecked = checked;
    }

    @Override
    public void setMessageNumber(int number) {
        mMessages.setMessageNumber(number);
    }

    @Override
    public void setHasMessage(boolean hasMessage) {
        mMessages.setHasMessage(hasMessage);
    }

    @Override
    public void setTitle(String title) {
        //mTitle.setText(title);
    }

    @Override
    public void setDefaultDrawable(Drawable drawable) {
        mDefaultDrawable = drawable;
        if (!mChecked) {
            if(!TextUtils.isEmpty(mLottiePath)){
                mIcon.clearAnimation();
            }
            mIcon.setImageDrawable(drawable);
        }
    }

    @Override
    public void setSelectedDrawable(Drawable drawable) {
        mCheckedDrawable = drawable;
        if (mChecked) {
            mIcon.setImageDrawable(drawable);
        }
    }

    @Override
    public String getTitle() {
        return mTitle.getText().toString();
    }

    public void setTextDefaultColor(@ColorInt int color) {
        mDefaultTextColor = color;
    }

    public void setTextCheckedColor(@ColorInt int color) {
        mCheckedTextColor = color;
    }
}
